package com.lagou.mvc.framework.util;

import java.io.File;
import java.util.List;

/**
 * @ClassName PackageUtil
 * @Description
 * @Author playboy
 * @Date 2021/4/29 4:53 下午
 * @Version 1.0
 **/
public class PackageUtil {


	/**
	 * @param classes
	 * @param basePackage com.lagou.edu.controller
	 */
	public static void scan(List<Class<?>> classes, String basePackage) {
		String path = PackageUtil.class.getClassLoader().getResource("").getPath() + File.separator + basePackage.replace(".", File.separator);
		File file = new File(path);
		File[] files = file.listFiles();
		for (File childFile : files) {
			if (childFile.isDirectory()) {
				String name = file.getName();
				scan(classes, basePackage + "." + name);
			} else {
				//文件
				if (childFile.getName().endsWith(".class")) {
					String className = basePackage + "." + childFile.getName().replace(".class", "");
					try {
						Class<?> aClass = Class.forName(className);
						classes.add(aClass);
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}


}
